let lastUserInfo;
$(function () {
    $("#btnChangInfo").click(function () {
        uploadUserInfo()
    })

    $("#userImage").hover(function () {
            $("#userImage").removeClass("border-0")
    },function () {
        $("#userImage").addClass("border-0")
    })

    lastUserInfo = $("#form").serialize();

    $(".userInfoClass").change(function () {
        changeInfo()
    })

    $("#userImage").click(function () {
        selectUserIcon()
    })
})
function selectUserIcon() {
    $("#upload").click()
}
function uploadUserInfo() {
    let form=jQuery("#form").serialize();
    // alert(form)
    if ($("#userName").text().length == 0 || $("#userName").text().length == 0 > 10) {
        alert("用户名输入为空或超出限制")
        return
    }
    if ($("#userPhone").text().length < 10 || $("#userPhone").text().length == 0 > 11) {
        alert("手机号输入不符")
        return
    }
    $.post("/demo/upDate/userInfo", form,function (rst) {
        alert(rst)
        // lastUserInfo = $("#form").serialize()
        window.location.reload()
    })
}
function inputUpload(){
    // alert("是否提交图片")
    if(confirm('是否提交图片？')) {
        let formData = new FormData($("#uploadForm")[0]);
        // formData.append("img",$("#uploadForm").file);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "/demo/upload", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function (rst) {
                // alert(rst[1])
                $("#userIconInput").val(rst[1])
                $("#userImage").attr("src", rst[1])
                changeInfo()
            },
            error: function (rst) {
                alert(rst)
            }
        });
    }
}
function changeInfo() {
    // alert("is ok")
    if (lastUserInfo != $("#form").serialize()) {
        $("#btnChangInfo").removeClass("disabled")
        $("#btnChangInfo").addClass("btn-outline-info")
    } else {
        $("#btnChangInfo").addClass("disabled")
        $("#btnChangInfo").removeClass("btn-outline-info")
    }
    $(this).value = ''
}
//model
let disable = false
let curCount
$(function () {
    $(".modelInput").change(function () {
        modelInfoChange()
    })
    $("#btnUpdatePwd").click(function () {
        updatePwd()
    })
    $("#getEmail").click(function () {
        if (disable) return;
        // alert("ok")
        $.post("/demo/email","userEmail="+$("#userEmail").text()+"&checkUser=true",function (rst) {
            // alert(rst)
            // 设置button效果，开始计时
        })
        document.getElementById("getEmail").setAttribute("disabled","true" );//设置按钮为禁用状态
        disable = true;
        curCount = 60
        document.getElementById("getEmail").innerText =curCount + "秒后重获";//更改按钮文字
        InterValObj = window.setInterval(SetRemainTime, 1000); // 启动计时器timer处理函数，1秒执行一次
    })
    $("#selectPwdCheck").click(function () {
        if ($("#progress").attr("settle") != 0) {
            let style = $("#progress").attr("style")
            let other = style.substr(style.indexOf(';'))
            if ($("#progress").attr("settle") != 0) {
                $("#progress").attr("style", "margin-left:0px" + other)
                $("#progress").attr("settle", 0)
            }
            $("#pwdCheck").attr("style","")
            $("#emailCheck").attr("style","display: none;")
        }
    })
    $("#selectEmailCheck").click(function () {
        if ($("#progress").attr("settle") == 0) {
            let style = $("#progress").attr("style")
            let other = style.substr(style.indexOf(';'))
            if ($("#progress").attr("settle") != 1) {
                $("#progress").attr("style","margin-left:96px"+other)
                $("#progress").attr("settle",1)
            }
            $("#pwdCheck").attr("style","display: none;")
            $("#emailCheck").attr("style","")
        }

    })
})
function SetRemainTime() {
    if (curCount == 0) {//超时重新获取验证码
        window.clearInterval(InterValObj);// 停止计时器
        document.getElementById("getEmail").removeAttribute("disabled");//移除禁用状态改为可用
        disable = false
        document.getElementById("getEmail").innerText="重获验证码";
    }else {
        curCount--;
        document.getElementById("getEmail").innerText=curCount + "秒后重获";
    }
}
function isPwd(Pwd) {
    return Pwd.length >= 2 && Pwd.length <= 20
}
function updatePwd() {
    if ($("#progress").attr("settle") == "1") {
        $("#oldPwd").val("")
    }
    let form=jQuery("#modelInputForm").serialize();
    // alert(form)
    // alert(form)
    $.post("/demo/upDate/userPwd", form,function (rst) {
        alert(rst)
        if(rst == "success") {
            $.removeCookie('userId',{ path: '/demo'});
            window.location.reload()
        } else {
            $("#modelMsg").removeClass("text-hide")
            $("#modelMsg").text(rst)
        }
        // lastUserInfo = $("#form").serialize()

    })
}
function modelInfoChange() {
    // alert("is ok")
    let PwdIsOk = false;
    let msg
    let oldPwd = $("#oldPwd").val()
    let newPwd = $("#newPwd").val()
    let ackPwd = $("#ackPwd").val()
    if ($("#progress").attr("settle") == "1" || isPwd(oldPwd)) {
        if(isPwd(newPwd)) {
            if (ackPwd == newPwd) {
                PwdIsOk = true
            } else msg = "新密码不相同"
        } else msg = "新密码密码长度2~20"
    } else msg = "原密码密码长度2~20"
    if (PwdIsOk) {
        $("#btnUpdatePwd").removeClass("disabled")
        $("#btnUpdatePwd").addClass("btn-outline-info")
        $("#modelMsg").addClass("text-hide")
    } else {
        $("#btnUpdatePwd").addClass("disabled")
        $("#btnUpdatePwd").removeClass("btn-outline-info")

        $("#modelMsg").removeClass("text-hide")
        $("#modelMsg").text(msg)
    }
    $(this).value = ''
}